<?php

namespace app\index\service;

use app\common\dao\SettingDao;
use app\common\model\AccessLog;
use app\common\model\Area;
use app\common\model\ConsultingQuestion;
use app\common\model\ConsultingServer;
use app\common\model\ConsultingServerSku;
use app\common\service\ARYPushQueueService;
use app\index\model\UM as UM;
use ShidianUtil\Exception\DataNotExistException;
use app\index\dao\LandingDao;
use app\common\model\Landing;
use think\Collection;
use think\exception\ValidateException;

/**
 *
 * Class LandingService
 * @package app\index\service
 */
class LandingService
{

    /**
     *
     * @param $id
     * @param $skuId
     * @return ConsultingServer|null
     */
    public function getServer($id, $skuId):?ConsultingServer
    {
        // 服务
        $server = ConsultingServer::where('id', $id)
            ->field(['id', 'showPrice', 'price', 'name', 'icon'])
            ->withAttr([
                'price' => fn($value) => (float)$value,
                'showPrice' => fn($value) => (float)$value,
            ])
            ->append(['iconImg'])
            ->find();

        if(!$server){
            return null;
        }

        $server->consultingServerSkuId = '0';

        if(is_id($skuId)){

            $sku = ConsultingServerSku::where('id', $skuId)
                ->where('consultingServerId', $server->id)
                ->where('deleted', '0')
                ->withAttr([
                    'price' => fn($value) => (float)$value,
                    'showPrice' => fn($value) => (float)$value,
                ])
                ->find();

            if($sku){
                $temp = clone $server;

                foreach ($temp->getData() as $field => $value) {
                    if (isset($sku->$field)) {
                        $temp->$field = $sku->$field;
                    }
                }

                $temp->consultingServerSkuId = $sku->id;
                $temp->id = $server->id;

                return $temp;
            }
        }

        return $server;
    }

}
